package com.huixin.powermonitor.common;

import com.huixin.powermonitor.entity.Bike;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;

public class CodeUtils {

    //订单编号生成规则：NP+时间戳（精确到毫秒）+两位随机数
    public static String orderCode() {
        String str = "NP" + System.currentTimeMillis();
        str += (int) ((Math.random() * 9 + 1) * 100);
        return str;
    }
    //计算订单金额
    public static BigDecimal calacMoney(Integer useTime, Bike bike) {
        BigDecimal firstPrice = bike.getFirstPrice();
        BigDecimal price = bike.getPrice();

        if (useTime <= bike.getFirstDistance()) {
            return firstPrice;
        }else{
            BigDecimal additionalTime = BigDecimal.valueOf( useTime - bike.getFirstDistance());
            BigDecimal additionalCost = additionalTime.multiply(price);
            return firstPrice.add(price);
        }
    }

    //计算用户时长（分钟）
    public static int calculateAndSetUserTime(LocalDateTime finishTime, LocalDateTime createTime) {
        //计算用户时长（分钟）
        Duration duration = Duration.between(createTime, finishTime);
        //获取秒差值，然后转换为分钟
        long secondsDifference = duration.getSeconds();
        int minutesDifference = (int) (secondsDifference / 60);

        return minutesDifference; //返回用户时长（分钟）
    }
}
